var app = getApp()

import {
    list as signInList
  } from '../../api/signIn'
import {
    summary,
} from '../../api/order'
import {
    unReadCount,
  } from '../../api/msgPush'
Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        shipped: 0,
        unshipping: 0,
        completed: 0,
        unpaid: 0,
        refunds: 0,
        showShipped: 0,
        showUnshipping: 0,
        showCompleted: 0,
        showUnpaid: 0,
        showRefunds: 0,
        unReadNum:"", //未读消息数
        showUnReadNum: "", //显示的未读数量
    },

    lifetimes: {
        attached: function() {
            // 在组件实例进入页面节点树时执行
            //可视窗口x,y坐标
        },
        detached: function() {
            // 在组件实例被从页面节点树移除时执行
        },
    },

    pageLifetimes: {
        show: function() {
            // 页面被展示
            console.info("页面被展示了")
            this.getOrderCount()
            this.getunReadCount()
        },
        hide: function() {
            // 页面被隐藏
        },
        resize: function(size) {
            // 页面尺寸变化
        }
    },

    /**
     * 组件的方法列表
     */
    methods: {
        getunReadCount() {
            unReadCount().then(
              res => {
                console.log("接收用户信息--" + JSON.stringify(res))
                if (res.type == 'success') {
                  let unReadNum = 0
                  if (res.data.length > 0) {
                    res.data.map(item => {
                      unReadNum += item.msg_num
                    })
                  }
                  this.setData({
                    unReadNum: unReadNum,
                    showUnReadNum: unReadNum > 100 ? '...' : unReadNum
                  })
                  console.log("unReadNum--" + this.data.unReadNum)
                }
              }
            ).catch(error => {
        
            })
          },
        // 推广中心
        toShareCenter() {
            this.checkLogin(() => {
                console.log("321321")
                wx.navigateTo({
                    // url: '/pages/member/shareCenter/shareCenter',
                    url: '/pages/member/share/share',
                })
            })

        },
        messageClick() {
            this.checkLogin(() => {
              wx.navigateTo({
                url: '/pages/member/message/index',
              })
            })

          },
     //收货地址
        toAddress: function (e) {
            this.checkLogin(() => {
            wx.navigateTo({
                url: '/pages/member/address/address',
                success: function (res) { },
                fail: function (res) { },
                complete: function (res) { },
            })
            })
        },
        //获取订单数量信息
        getOrderCount() {
            summary().then(
                res => {
                    console.log("接收订单数量信息--" + JSON.stringify(res))
                    if (res.type == 'success') {
                        this.setData({
                            shipped: res.data.shipped,
                            unshipping: res.data.unshipping,
                            completed: res.data.completed,
                            unpaid: res.data.unpaid,
                            refunds: res.data.refunds,
                            showShipped: res.data.shipped > 99 ? '...' : res.data.shipped,
                            showUnshipping: res.data.unshipping > 99 ? '...' : res.data.unshipping,
                            showCompleted: res.data.completed > 99 ? '...' : res.data.completed,
                            showUnpaid: res.data.unpaid > 99 ? '...' : res.data.unpaid,
                            showRefunds: res.data.refunds > 99 ? '...' : res.data.refunds
                        })
                    }
                }
            ).catch(error => {
                console.log(error)
            })
        },
        //跳转到所有的订单页面
        toOrder: function(e) {
            this.checkLogin(() => {
                let param = e.currentTarget.dataset.index
                console.info(param)
                wx.navigateTo({
                    url: '/pages/member/order/index?index=' + 0,
                })
            })
        },
        toOrderRefund: function() {
            this.checkLogin(() => {
                wx.navigateTo({
                    url: '/pages/member/refund/list',
                })
            })

        },
        toUnpaid: function(e) {
            this.checkLogin(() => {

                signInList().then(res=>{

                    if(res.data.data.length>0){
                        wx.navigateTo({
                            url: '/pages/member/function/signIn?index=' + 1,
                        })
                       } else{
                        wx.showToast({
                            icon: 'none',
                            title: '暂无活动',
                          })
                       }
                }).catch(res=>{
                    wx.showToast({
                        icon: 'none',
                        title: '暂无活动',
                      })

                })
            })

        },
        toNotDelivered: function(e) {
            this.checkLogin(() => {
                wx.navigateTo({
                    url: '/pages/member/order/index?index=' + 2,
                })
            })
        },
        toBeReceived: function(e) {
            this.checkLogin(() => {
                wx.navigateTo({
                    url: '/pages/member/order/index?index=' + 3,
                })
            })
        },
        toEvaluate: function(e) {
            this.checkLogin(() => {
                wx.navigateTo({
                    url: '/pages/member/order/index?index=' + 4,
                })
            })
        },
        //检测用户的登陆状态
        checkLogin(callback) {
            console.info("全局变量app.globalData--" + JSON.stringify(app.globalData))
            if (app.globalData.token == null) {
                wx.navigateTo({
                    url: '/pages/login/index',
                })
            } else {
                if (callback) {
                    callback()
                }
            }
        },
    }
})
